//
//  HomePageViewController.m
//  MemberProject
//
//  Created by MAC OS X on 15/4/2.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "HomePageViewController.h"
#import "HomePageTableViewCell.h"
#import "HomePageSectionHeaderTableViewCell.h"
#import "SystemNotificationDetailViewController.h"
#import "SystemNotificationListViewController.h"
#import "FlashNewDetailViewController.h"
#import "FlashNewListViewController.h"
#import "MyInfoDetailViewController.h"
#import "MyInfoListViewController.h"
#import "AdScrollView.h"
#import "CityPositionViewController.h"
@interface HomePageViewController ()<UITableViewDataSource,UITableViewDelegate,HomePageSectionHeaderTableViewCellDelegate>
//系统通知
@property(strong,nonatomic)NSArray *systemNotificationData;
//快讯播报
@property(strong,nonatomic)NSArray *flashNewData;
//我的信息
@property(strong,nonatomic)NSArray *myInfoData;

@property(strong,nonatomic)NSArray *images;
@property(strong,nonatomic)UITableView *tableView;

@property(strong,nonatomic)AdScrollView *adScrollView;
@end

@implementation HomePageViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    self.title=@"首页";
    
//    UIBarButtonItem *leftBarButton=[[UIBarButtonItem alloc]initWithTitle:@"定位" style:UIBarButtonItemStyleBordered target:self action:@selector(chickLeftBarButton:)];
//    self.navigationItem.leftBarButtonItem=leftBarButton;
    
    
    AdScrollView *adScrollView=[[AdScrollView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, (SCREEN_HEIGHT-20)/7)];
    if(SCREEN_HEIGHT>568){
        adScrollView.height=(SCREEN_HEIGHT-64-49-5*5-((SCREEN_HEIGHT-20)/7))/4;
    }
    adScrollView.data=@[@"guanggao_4.jpg",@"guanggao_0.jpg",@"guanggao_1.jpg",@"guanggao_2.jpg",@"guanggao_3.jpg",@"guanggao_4.jpg",@"guanggao_0.jpg"];
    [self.view addSubview:adScrollView];
    self.adScrollView=adScrollView;
    
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, adScrollView.y+adScrollView.height, SCREEN_WIDTH,SCREEN_HEIGHT-49-64-adScrollView.height) style:UITableViewStylePlain];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableView=tableView;
    [self.view addSubview:tableView];
    
    [tableView registerNib:[UINib nibWithNibName:@"HomePageSectionHeaderTableViewCell" bundle:nil] forCellReuseIdentifier:@"headerCellIdentifier"];
    [tableView registerNib:[UINib nibWithNibName:@"HomePageTableViewCell" bundle:nil] forCellReuseIdentifier:@"CellIdentifier"];
    
    self.systemNotificationData=@[@{@"title":@"",@"context":@"",@"time":@""}];
    self.flashNewData=@[@{@"title":@"",@"context":@"",@"time":@""}];
    self.myInfoData=@[@{@"title":@"",@"context":@"",@"time":@""}];
    self.images=@[[UIImage imageNamed:@"设置.jpg"],[UIImage imageNamed:@"全网.jpg"],[UIImage imageNamed:@"机构.jpg"]];
}

#pragma mark - LeftBarButton
-(void)chickLeftBarButton:(UIBarButtonItem *)sender{
    CityPositionViewController *vc=[[CityPositionViewController alloc]init];
    BaseNavigationController *nav=[[BaseNavigationController alloc]initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark - tableView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row!=0){
        if(indexPath.section==0){
            //系统通知
            SystemNotificationDetailViewController *vc=[[SystemNotificationDetailViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if(indexPath.section==1){
            //快讯播报
            FlashNewDetailViewController *vc=[[FlashNewDetailViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        if(indexPath.section==2){
            //我的信息
            MyInfoDetailViewController *vc=[[MyInfoDetailViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0:{
            return self.systemNotificationData.count+1;
        }
            break;
        case 1:{
            return self.flashNewData.count+1;
        }
            break;
        case 2:{
            return self.myInfoData.count+1;
        }
            break;
        default:
            break;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *headerCellIdentifier=@"headerCellIdentifier";
    static NSString *CellIdentifier=@"CellIdentifier";
    if(indexPath.section==0){
        //系统通知
        if(indexPath.row==0){
            HomePageSectionHeaderTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:headerCellIdentifier forIndexPath:indexPath];
            cell.label.text=@"系统通知";
            cell.delegate=self;
            cell.sectionImgaeView.image=self.images[indexPath.section];
            return cell;
        }
        HomePageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.titleLabel.text=self.systemNotificationData[indexPath.row-1][@"title"];
        cell.contextLabel.text=self.systemNotificationData[indexPath.row-1][@"context"];
        cell.timeLabel.text=self.systemNotificationData[indexPath.row-1][@"time"];
        if(indexPath.row-1==self.systemNotificationData.count-1){
            cell.lineLabel.hidden=YES;
        }else{
            cell.lineLabel.hidden=NO;
        }
        return cell;
    }
    if(indexPath.section==1){
        //快讯播报
        if(indexPath.row==0){
            HomePageSectionHeaderTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:headerCellIdentifier forIndexPath:indexPath];
            cell.label.text=@"快讯播报";
            cell.delegate=self;
            cell.sectionImgaeView.image=self.images[indexPath.section];
            return cell;
        }
        HomePageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.titleLabel.text=self.flashNewData[indexPath.row-1][@"title"];
        cell.contextLabel.text=self.flashNewData[indexPath.row-1][@"context"];
        cell.timeLabel.text=self.flashNewData[indexPath.row-1][@"time"];
        if(indexPath.row-1==self.flashNewData.count-1){
            cell.lineLabel.hidden=YES;
        }else{
            cell.lineLabel.hidden=NO;
        }
        return cell;
    }
    if(indexPath.section==2){
        //我的信息
        if(indexPath.row==0){
            HomePageSectionHeaderTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:headerCellIdentifier forIndexPath:indexPath];
            cell.label.text=@"我的信息";
            cell.delegate=self;
            cell.sectionImgaeView.image=self.images[indexPath.section];
            return cell;
        }
        HomePageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        cell.titleLabel.text=self.myInfoData[indexPath.row-1][@"title"];
        cell.contextLabel.text=self.myInfoData[indexPath.row-1][@"context"];
        cell.timeLabel.text=self.myInfoData[indexPath.row-1][@"time"];
        if(indexPath.row-1==self.myInfoData.count-1){
            cell.lineLabel.hidden=YES;
        }else{
            cell.lineLabel.hidden=NO;
        }
        return cell;
    }
    return nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row==0){
        return 37.0f;
    }else{
        if(SCREEN_HEIGHT>568){
            return (SCREEN_HEIGHT-64-49-37*3-7*2-self.adScrollView.height)/(self.systemNotificationData.count+self.flashNewData.count+self.myInfoData.count);
        }else{
            return 64.0f;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if(section!=2){
        return 7;
    }
    return 0;
}

/*
#pragma mark - scrollView
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if(scrollView==self.scrollView){
        if([self.timer isValid]){
            [self.timer invalidate];
        }
    }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(scrollView==self.scrollView){
        if([self.timer isValid]){
            [self.timer invalidate];
        }
        self.timer=[NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(scrollView==self.scrollView){
        if(scrollView.contentOffset.x>=scrollView.contentSize.width-(SCREEN_WIDTH)){
            [scrollView setContentOffset:CGPointMake((SCREEN_WIDTH), 0)];
            return;
        }
        if(scrollView.contentOffset.x<=0){
            [scrollView setContentOffset:CGPointMake(scrollView.contentSize.width-2*(SCREEN_WIDTH), 0)];
            return;
            
        }
        
    }
}

-(void)timer:(NSTimer *)sender{
    int nowPage=self.scrollView.contentOffset.x/(SCREEN_WIDTH);
    [self.scrollView setContentOffset:CGPointMake((SCREEN_WIDTH)*(nowPage+1), 0) animated:YES];
}
*/
#pragma mark - HomePageSectionHeaderTableViewCell
-(void)chickMoreButtonWithHomePageSectionHeaderTableViewCell:(HomePageSectionHeaderTableViewCell *)cell{
    NSIndexPath *indexPaht=[self.tableView indexPathForCell:cell];
    if(indexPaht.section==0){
        //系统通知
        SystemNotificationListViewController *vc=[[SystemNotificationListViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPaht.section==1){
        //快讯
        FlashNewListViewController *vc=[[FlashNewListViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    if(indexPaht.section==2){
        //我的信息
        MyInfoListViewController *vc=[[MyInfoListViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

@end
